<!DOCTYPE html>
<html>
  <head>
    <title>Audio Output Devices</title>
    <script src="../../resources/testharness.js"></script>
    <script src="../../resources/testharnessreport.js"></script>
  </head>
  <body>
    <script>
      // TODO(guidou): Add more tests with nonempty src once a MockWebMediaPlayer 
      // is available int the test runner. See crbug.com/546566.
      
      // Tests that the API is available.
      test(function() {
        var audio = document.createElement('audio');
        assert_not_equals(audio, null);
        assert_idl_attribute(audio, 'setSinkId');
        assert_equals(typeof audio.setSinkId, 'function');
        assert_idl_attribute(audio, 'sinkId');
        assert_equals(audio.sinkId, '');
      }, 'Basic API test');

      // Tests that setting the sink to the default device succeeds.
      promise_test(function() {
        var audio = document.createElement('audio');
        return audio.setSinkId('')
      }, 'setSinkId("")');

      // Tests that setting the sink to a valid device succeeds.
      // Note: 'valid' is the name of a valid device in the test runner.
      promise_test(function() {
        var audio = document.createElement('audio');
        var p = audio.setSinkId('valid')
          .then(function() {
            assert_equals(audio.sinkId, 'valid');
          });
        assert_equals(audio.sinkId, '');  // sinkId not updated yet
        return p;
      }, 'setSinkId("valid")');

      // Test that changing the src on an element and calling setSinkId() works
      promise_test(function() {
        var audio = document.createElement('audio');
        audio.src = '../content/test.oga';
        return audio.setSinkId('');

      }, 'change src, then setSinkId("")');

      // Tests that setting the sink to an unauthorized device fails.
      // Note: 'unauthorized' is the name of an unauthorized device in the test runner.
      promise_test(function() {
        var audio = document.createElement('audio');
        return promise_rejects_dom(this, 'SecurityError', audio.setSinkId('unauthorized'));
      }, 'setSinkId("unauthorized")' );

      // Test that setting the sink to a nonexistent device fails
      promise_test(function() {
        var audio = document.createElement('audio');
        return promise_rejects_dom(this, 'NotFoundError', audio.setSinkId('nonexistent'));
      }, 'setSinkId("nonexistent")');

      // Tests that changing the sink twice succeeds
      promise_test(function() {
        var audio = document.createElement('audio');
        return audio.setSinkId('valid')
          .then(function() {
            assert_equals(audio.sinkId, 'valid');
            return audio.setSinkId('')
              .then(function() {
                assert_equals(audio.sinkId, '');
              });
          });
      }, 'setSinkId("valid") followed by setSinkId("").');

    </script>
  </body>
</html>
